package fr.afcepf.atod18.utilitaires.web;

import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;
import javax.xml.ws.BindingProvider;

import org.springframework.transaction.annotation.Transactional;

import fr.afcepf.atod18.restaurants.web.ListeRestaurantMB;
import fr.afcepf.atod18.service.ConversionDevise;
import fr.afcepf.atod18.service.ConversionDeviseImplService;
import fr.afcepf.atod18.service.Devise;
import fr.afcepf.atod18.service.Pays;
import fr.afcepf.atod18.utilitaires.entity.InfoSession;

@ManagedBean(name = "deviseMB")
@SessionScoped
@Transactional
public class DeviseMB {
	private List<Pays> listePays;
	private Devise selDevise;
	private int idSelPays;
	private InfoSession infoSession;

    @ManagedProperty(value="#{listeRestaurantMB}")
    private ListeRestaurantMB listeRestaurantMB;
    
    /**
	 * @return the selectedDevise
	 */
	public Devise getSelDevise() {
		return selDevise;
	}

	/**
	 * @param selDevise the selectedDevise to set
	 */
	public void setSelDevise(Devise selDevise) {
		this.selDevise = selDevise;
	}

	/**
	 * @return the listePays
	 */
	public List<Pays> getListePays() {
		return listePays;
	}

	/**
	 * @param listePays the listePays to set
	 */
	public void setListePays(List<Pays> listePays) {
		this.listePays = listePays;
	}
    public DeviseMB() {
        super();
        System.out.println("build deviseMB");
    }
    
	public void initListePays(ComponentSystemEvent event){

		ConversionDevise deviseWS = (new ConversionDeviseImplService()).getConversionDeviseImplPort();
		System.out.println("init");
		javax.xml.ws.BindingProvider bp = (javax.xml.ws.BindingProvider) deviseWS;
		Map<String,Object> context = bp.getRequestContext();
		context.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
				"http://localhost:8080/devises/services/Conversion");
//				"http://192.168.30.32/devises/services/Conversion");
	//http://localhost:8080/devises/services/Conversion?wsdl
		
		listePays=deviseWS.listerPays("EUR");
   	
//    	try {
//			URL url= new URL("http://localhost:8080/devises/services/Conversion/listerPays?monnaieSource='EUR'");
//			HttpURLConnection connex = (HttpURLConnection) url.openConnection();
//			connex.setRequestMethod("GET");
//			connex.setRequestProperty("Accept", "application/Conversion");
//			
//			if( connex.getResponseCode() != 200){
//				throw new RuntimeException("Failed : Http error Code " + connex.getResponseCode());
//			}
//			
//			BufferedReader reader = new BufferedReader(new InputStreamReader(connex.getInputStream()));
//			String output;
//			while ((output = reader.readLine()) != null){
//				System.out.println(output);
//			}
//			connex.disconnect();
//			
//		} catch (MalformedURLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}
    
    public String doSelection() {
    	System.out.println("pays "+ idSelPays);
    	for(Pays p: listePays){
    		if(p.getId()==idSelPays){
    			selDevise = p.getDevise();
     			System.out.println("devise " + selDevise.getLibelle() + " de taux "+ selDevise.getTaux());
     			listeRestaurantMB.getInfoSession().setDevise(p.getDevise());
     			break;
    		}
    	}
		FacesContext fc = FacesContext.getCurrentInstance();
		if(fc !=null && fc.getExternalContext()!=null){
			infoSession=(InfoSession)fc.getExternalContext().getApplicationMap().get("infoSession");
		}


    	if (infoSession.isEstConnecte()== false){
    		return "AccueilVisiteur";
    	}
    	else {
    		System.out.println("Page courante = "+infoSession.getPageCourante());
    		return infoSession.getPageCourante()+"Client.jsf";
    	}
   }

	/**
	 * @return the idSelPays
	 */
	public int getIdSelPays() {
		return idSelPays;
	}

	/**
	 * @param idSelPays the idSelPays to set
	 */
	public void setIdSelPays(int idSelPays) {
		this.idSelPays = idSelPays;
	}

	/**
	 * @return the listeRestaurantMB
	 */
	public ListeRestaurantMB getListeRestaurantMB() {
		return listeRestaurantMB;
	}

	/**
	 * @param listeRestaurantMB the listeRestaurantMB to set
	 */
	public void setListeRestaurantMB(ListeRestaurantMB listeRestaurantMB) {
		this.listeRestaurantMB = listeRestaurantMB;
	}
	
}
